﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.BLL;
using System.Web.Script.Serialization;
using Maticsoft.Model.Register;
using Register.Model;
using System.Text;

namespace RegisterWeb.Manager.RegistrationFeeManager.ashx
{
    /// <summary>
    /// RegistrationFeeManagerService 的摘要说明
    /// </summary>
    public class RegistrationFeeManagerService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            action = action;
            if (action == "get")
            {
                int flag = 1;
                int intPageSize = int.Parse(context.Request["rows"].ToString());
                int intCurrentPage = int.Parse(context.Request["page"].ToString());
                string Hospital_ID = (string)context.Request["hpname"];
                string RegistrationFee_OutpatientType = (string)context.Request["rfoutpatientype"];
                StringBuilder strBuilder = new StringBuilder();
                if (!String.IsNullOrEmpty(Hospital_ID))
                {
                    strBuilder.Append(" Hospital_ID=").Append(Hospital_ID).Append(" ");
                    flag = 0;
                }
                if (!String.IsNullOrEmpty(RegistrationFee_OutpatientType))
                {
                    if (flag == 1)
                    {
                        strBuilder.Append(" RegistrationFee_OutpatientType='").Append(RegistrationFee_OutpatientType).Append("' ");
                    }
                    else
                    strBuilder.Append(" and RegistrationFee_OutpatientType='").Append(RegistrationFee_OutpatientType).Append("' ");
                    flag = 0;
                }
                if (flag == 1)
                {
                    strBuilder.Append("1=1");
                }
                //if (Hospital_ID != "")
                //{
                //    sql=sql+"Hospital_ID=" +Hospital_ID;
                //    flag = 0;
                //}
                //if (RegistrationFee_OutpatientType != "")
                //{
                //       sql=sql+"RegistrationFee_OutpatientType=" + RegistrationFee_OutpatientType;
                //        flag = 0;     
                //}
                //if (flag == 1)
                //{
                //    sql = "1=1";
                //}

                var registrationFeelist = RegistrationFeeInfoBLL.GetAllRegistrationFee(strBuilder.ToString(), intCurrentPage, intPageSize);
                List<RegistrationFeeListJSON> listjson = new  List<RegistrationFeeListJSON>();
             
                foreach(var row in  registrationFeelist)
                    {   RegistrationFeeListJSON RegistrationFee = new RegistrationFeeListJSON();
                        HospitalInfo hospitallnfo = HospitalInfoBLL.GetHospitalInfoByID(row.Hospital_ID);
                        RegistrationFee.HospitalIname = hospitallnfo.Hospital_Name;
                        RegistrationFee.Hospital_ID = row.Hospital_ID;
                        RegistrationFee.RegistrationFee_AddTime = row.RegistrationFee_AddTime;
                        RegistrationFee.RegistrationFee_ID = row.RegistrationFee_ID;
                        RegistrationFee.RegistrationFee_LastAmendPerson = row.RegistrationFee_LastAmendPerson;
                        RegistrationFee.RegistrationFee_LastAmendTime = row.RegistrationFee_LastAmendTime;
                        RegistrationFee.RegistrationFee_Menory = row.RegistrationFee_Menory;
                        RegistrationFee.RegistrationFee_OutpatientType = row.RegistrationFee_OutpatientType;
                        RegistrationFee.RegistrationFee_Recoder = row.RegistrationFee_Recoder;
                        RegistrationFee.RegistrationFee_Status = row.RegistrationFee_Status;
                        RegistrationFee.OutpatientTypename = ParamterHelper.GetParameterNameByTypeAndID("regType", row.RegistrationFee_OutpatientType);
                        listjson.Add(RegistrationFee);
                     }
            
                jsons list = new jsons();
                list.rows = listjson;
                list.total = RegistrationFeeInfoBLL.GetRoleCount("1=1");
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(list));
            }
            else if (action == "add")
            {
                RegistrationFee registrationFee = new RegistrationFee();

                registrationFee.Hospital_ID = (string)context.Request["selectHosptial"];
                registrationFee.RegistrationFee_AddTime = DateTime.Now;
                registrationFee.RegistrationFee_ID = DataIDHelper.GetDataID("RegistrationFee_ID");
                registrationFee.RegistrationFee_LastAmendPerson = "";
                registrationFee.RegistrationFee_LastAmendTime = DateTime.Now;
                registrationFee.RegistrationFee_Recoder = "1";//以后用SESSTION
                registrationFee.RegistrationFee_Menory = Convert.ToDecimal(context.Request["RegistrationFee_Menory"]);
                registrationFee.RegistrationFee_OutpatientType = (string)context.Request["RegistrationFee_OutpatientType"];
                registrationFee.RegistrationFee_Status = "1";   
                JavaScriptSerializer jss = new JavaScriptSerializer();  
                string j = jss.Serialize(new message(true, "价格添加成功"));
                if(RegistrationFeeInfoBLL.Add(registrationFee))
                context.Response.Write(j);
                context.Response.End();
            }
            else if (action == "delete")
            {
                String RegistrationFee_ID = context.Request["RegistrationFee_ID"];
                if(RegistrationFeeInfoBLL.Delete(RegistrationFee_ID))
                context.Response.Write("ok");
                else
                context.Response.Write("no");
            }
            else if (action == "updata")
            {
                RegistrationFee registrationFee = new RegistrationFee();
                registrationFee.Hospital_ID = (string)context.Request["selectHosptial"];
           //   registrationFee.RegistrationFee_AddTime = Convert.ToDateTime((string)context.Request["RegistrationFee_AddTime"]);
                registrationFee.RegistrationFee_ID = (string)context.Request["RegistrationFee_ID"];
                registrationFee.RegistrationFee_LastAmendPerson = "1";
                registrationFee.RegistrationFee_LastAmendTime = DateTime.Now;
                //registrationFee.RegistrationFee_Recoder = "1";//以后用SESSTION
                registrationFee.RegistrationFee_Menory = Convert.ToDecimal(context.Request["RegistrationFee_Menory"]);
                registrationFee.RegistrationFee_OutpatientType = (string)context.Request["RegistrationFee_OutpatientType"];
                registrationFee.RegistrationFee_Status = "1";
                JavaScriptSerializer jss = new JavaScriptSerializer();  
                string j = jss.Serialize(new message(true, "价格修改成功"));
                if (RegistrationFeeInfoBLL.Update(registrationFee))
                context.Response.Write(j);
                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    class jsons
    {
        public int total { get; set; }
        public List<RegistrationFeeListJSON> rows { get; set; }
    }
    class RegistrationFeeListJSON : RegistrationFee
    {
        private string HospitalI_name;
        private string OutpatientType_name;
        public string HospitalIname
        {
            set { HospitalI_name = value; }
            get { return HospitalI_name; }
        }
        public string OutpatientTypename
        {
            set { OutpatientType_name = value; }
            get { return OutpatientType_name; }
        }
    }

}